<?php
namespace Admin\Controller;
/**
 * 厂家收货地址
 * User: linkwhy
 * Date: 2015/11/16
 * Time: 12:05
 */
class VenderController extends AdminController{
    public function index(){
        $ca = M('clients_address');
        $where['type'] = 1;
        if( !IS_ADMIN ){
            $where['class_id'] = CLASS_ID;
        }
        $list = $ca->alias('ca')->field('ca.id,ca.name,phone,address,code,type,g.name as gname')->join('__GOODS_CLASS__ g ON ca.class_id = g.id', 'left')->where( $where )->select();
        $this->meta_title = '厂家收货地址';
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        $this->meta_title = '新增地址';
        $this->classes = getClassNames();
        $this->display('update');
    }

    public function edit(){
        $brand = M('clients_address');
        $data = $brand->field('id,name,phone,address,code,class_id')->where(array('id' => I('get.id')))->find();
        $this->meta_title = '修改地址';
        $this->classes = getClassNames();
        $this->assign('data', $data);
        $this->display('update');
    }

    public function update(){
        $ca = M('clients_address');
        $count = $ca->where("type = 1")->count();
        $postData = array(
            'name' => I('post.name'),
            'phone' => I('post.phone'),
            'code' => I('post.code'),
            'address' => I('post.address'),
            'type' => 1
        );
        if($count > 0){
            if( $ca->where( "type = 1" )->save($postData) ){
                $this->success('修改成功！', U("index"));
            }
        }else{
            $ca->create($postData);
            if( $ca->add() ){
                $this->success('添加成功！', U("index"));
            }
        }
    }
}